package com.sw.youvote.client.services;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sw.youvote.client.QuizListGUI;
import com.sw.youvote.shared.objects.CandidatureBean;
import com.sw.youvote.shared.objects.QuizBean;

public class QuizServiceDelegate {
    private QuizServiceAsync quizService = GWT.create(QuizService.class);
    private QuizListGUI gui;
    
    public void listQuizs() {
    	quizService.listQuizs(new AsyncCallback<List<QuizBean>> () {
                    public void onFailure(Throwable caught) {
                        gui.service_eventListQuizsFailed(caught);
                    }
        
                    public void onSuccess(List<QuizBean> result) {
                        gui.getContentArea().service_eventListRetrievedFromService(result);
                        
                    }
        }//end of inner class
        );//end of listQuizs method call.
    }
    
    public void addQuiz(final QuizBean quiz) {
        quizService.addQuiz(quiz, new AsyncCallback<Void> () {
            public void onFailure(Throwable caught) {
                gui.service_eventAddQuizFailed(caught);
            }

            public void onSuccess(Void result) {
                gui.service_eventAddQuizSuccessful();
            }
        }//end of inner class
        );//end of addQuiz method call.        
    }

    public void updateQuiz(final QuizBean quiz) {
        quizService.updateQuiz(quiz, new AsyncCallback<Void> () {
            public void onFailure(Throwable caught) {
                gui.service_eventUpdateQuizFailed(caught);
            }

            public void onSuccess(Void result) {
                gui.service_eventUpdateQuizSuccessful();
            }
        }//end of inner class
        );//end of updateQuiz method call.        
    }

    public void removeQuiz(final QuizBean quiz) {
        quizService.removeQuiz(quiz, new AsyncCallback<Void> () {
            public void onFailure(Throwable caught) {
                gui.service_eventRemoveQuizFailed(caught);
            }

            public void onSuccess(Void result) {
                gui.service_eventRemoveQuizSuccessful();
            }
        }//end of inner class
        );//end of updateQuiz method call.        
    }
    
    public void addCandidature(final QuizBean quiz, final CandidatureBean candidature) {
        quizService.addCandidature(quiz, candidature, new AsyncCallback<Void> () {
            public void onFailure(Throwable caught) {
                gui.service_eventAddCandidatureFailed(caught);
            }

            public void onSuccess(Void result) {
                gui.service_eventAddCandidatureSuccessful();
            }
        }//end of inner class
        );//end of addQuiz method call.        
    }
    
    public void listCandidatures(final QuizBean quiz) {
    	quizService.listCandidatures(quiz,new AsyncCallback<List<CandidatureBean>> () {
                    public void onFailure(Throwable caught) {
                        gui.service_eventListCandidaturesFailed(caught);
                    }
        
                    public void onSuccess(List<CandidatureBean> result) {
                        gui.service_eventListCandidaturesSucess(result);
                        
                    }
        }//end of inner class
        );//end of listQuizs method call.
    }
    
    public void setGUI(QuizListGUI newGUI){
    	this.gui = newGUI;
    }
    
    public QuizListGUI getGUI(){
    	return gui;
    }
    
}
